<?php


namespace app\service\utils;


use EasyWeChat\Factory;
use think\facade\Log;

class WeiXinService
{
    protected $app;

    public function __construct()
    {
        $appId = config('payment.miniprogram_app_id');
        $secret = config('payment.miniprogram_secret');

        $config = [
            'app_id' => $appId,
            'secret' => $secret,

            // 下面为可选项
            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',

            'log' => [
                'level' => 'debug',
                'file' => public_path() . 'wechat.log',
            ],
        ];
        $this->app = Factory::miniProgram($config);
    }

    /**
     * 微信授权
     * @param $code
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function wxAuth($code)
    {
        $result = $this->app->auth->session($code);
        if (isset($result['errcode']) && $result['errcode'] != 0) {
            throw new \Exception($result['errmsg']);
        }
        return $result;
    }

    /**
     * 小程序获取手机号
     * @string $code
     * @return array|\EasyWeChat\Kernel\Support\Collection|object|\Psr\Http\Message\ResponseInterface|string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function getMobile($code)
    {
        $result = $this->app->phone_number->getUserPhoneNumber($code);
        if (isset($result['errcode']) && $result['errcode'] != 0) {
            throw new \Exception($result['errmsg']);
        }
        return $result['phone_info']['phoneNumber'];
    }

    /**
     * 小程序码
     * @param $page
     * @param $uid
     * @return string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
     */
    public function getUserQrcode($page, $uid)
    {
        $response = $this->app->app_code->get($page);
        if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
            $path = '/qrcode/user/';
            $response->saveAs(public_path() . $path, $uid . '.png');
            return $path . $uid . '.png';
        } else {
            throw new \Exception('生成小程序码错误');
        }
    }

    /**
     * 小程序码
     * @param $page
     * @param $uid
     * @return string
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
     * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
     */
    public function getCoachQrcode($page, $uid)
    {
        $response = $this->app->app_code->get($page);
        if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
            $path = '/qrcode/coach';
            $response->saveAs(public_path() . $path, $uid . '.png');
            return $path . '/' . $uid . '.png';
        } else {
            throw new \Exception('生成小程序码错误');
        }
    }
}